package com.tongda.menu.activity;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tongda.core.common.Constants;
import com.tongda.core.common.KernalApplication;
import com.tongda.core.service.RestaurantService;
import com.tongda.core.utils.SharedPreferencesUtils;
import com.tongda.core.vo.Restaurant;
import com.tongda.menu.R;
import com.tongda.menu.R.drawable;
import com.tongda.menu.util.ActivityMenuUtil;

import android.R.style;
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources.Theme;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RstNewsActivity extends FragmentActivity{
		private TextView rstNameTv;
		private ViewPager rstPicVp;
		private TextView rstNoteTv;
		private TextView rstConTv;
		private TextView rstSpeTv;
		private TextView rstDneTv;
		private Button rstOrderBtn;
		
		private Restaurant rest;
		private TextView rstNotesLinkTv;
		private KernalApplication application;
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_rstnews);
			rstNameTv = (TextView)findViewById(R.id.rst_name);
			rstPicVp = (ViewPager)findViewById(R.id.rst_pic);
			rstNoteTv = (TextView)findViewById(R.id.rst_notes);
			rstConTv= (TextView)findViewById(R.id.rst_con_person);
			rstSpeTv = (TextView)findViewById(R.id.rst_specialEnvSb);
			rstDneTv = (TextView)findViewById(R.id.rst_diningEnvSb);
			rstOrderBtn = (Button)findViewById(R.id.rst_orderBtn);
			rstNotesLinkTv = (TextView)findViewById(R.id.rst_noteslink);
			application = KernalApplication.getInstance();
			rstOrderBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					RstNewsActivity.this.finish();
					RstNewsActivity.this.overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_top);
				}
			});
			
			new getRestInfoTask().execute();
		}
		
		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			if(keyCode==KeyEvent.KEYCODE_BACK){
				RstNewsActivity.this.finish();
				RstNewsActivity.this.overridePendingTransition(R.anim.in_from_bottom, R.anim.out_to_top);
			}
		return super.onKeyDown(keyCode, event);
		}
		
		public class getRestInfoTask extends AsyncTask<Void, Void, Restaurant>{

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
			}
			
			@Override
			protected Restaurant doInBackground(Void... params) {
				rest = RestaurantService.getRestInfo();
				return null;
			}
			
			@Override
			protected void onPostExecute(Restaurant result) {
				super.onPostExecute(result);
				
				if(rest.getRestName()!=null){
					rstNameTv.setText(rest.getRestName());
					SharedPreferencesUtils.setParam(RstNewsActivity.this, Constants.MENU_RES_NAME, rest.getRestName());
				}
				
				List<String> picUrls = rest.getRestPicUrls();
				if(picUrls!=null && picUrls.size()>0){
					PicAdapter picAdapter = new PicAdapter(getSupportFragmentManager(),picUrls);
					rstPicVp.setAdapter(picAdapter);
				}
				
				if(rest.getNotes()!=null){
					String notes = rest.getNotes();
					if(notes.toCharArray().length>200){
						String subNotes = notes.substring(0, 200);
						rstNoteTv.setText(subNotes+"......");
						rstNotesLinkTv.setVisibility(View.VISIBLE);
					}else{
						rstNoteTv.setText(notes);
						rstNotesLinkTv.setVisibility(View.INVISIBLE);
					}
				}
		        SpannableString sp = new SpannableString("更多......");               
		        sp.setSpan(new URLSpan(""), 0, sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);            
		        sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0, sp.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);             
		        //SpannableString对象设置给TextView            
		        rstNotesLinkTv.setText(sp);            
		        rstNotesLinkTv.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						final AlertDialog dialog = new AlertDialog.Builder(RstNewsActivity.this).create();
						View dialogView = View.inflate(RstNewsActivity.this, R.layout.dialog_restnews, null);
						ImageView cancelImv =  (ImageView)dialogView.findViewById(R.id.dialog_news_cancel);
						TextView titleTv = (TextView)dialogView.findViewById(R.id.dialog_news_title);
						TextView manTv = (TextView)dialogView.findViewById(R.id.dialog_news_creatMan);
						TextView timeTv = (TextView)dialogView.findViewById(R.id.dialog_news_creatTime);
						TextView contentTv = (TextView)dialogView.findViewById(R.id.dialog_news_content);
						LinearLayout subTitleTv = (LinearLayout)dialogView.findViewById(R.id.dialog_news_subTitle);
						
						titleTv.setText(rest.getRestName());
						subTitleTv.setVisibility(View.GONE);
						String content = rest.getNotes();
						contentTv.setText(content);
						cancelImv.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								dialog.dismiss();
							}
						});
						dialog.setView(dialogView, 0, 0, 0, 0);
						dialog.show();
					}
				});
				rstConTv.setText("人均消费："+Html.fromHtml("<font color='red'>"+"￥"+(rest.getConsPerPerson().equals("null")?"暂未公布哦......":rest.getConsPerPerson())+"</font>"));
				rstSpeTv.setText("特色环境："+rest.getSpecialEnvSb());
				rstDneTv.setText("就餐氛围："+rest.getDiningEnvSb());
				
			}
		}
		
		class PicAdapter extends FragmentPagerAdapter{
			
			List<String> picUrls;
			
			public PicAdapter(FragmentManager fm, List<String> picUrls) {
				super(fm);
				this.picUrls = picUrls;
			}
			@Override
			public Fragment getItem(int position) {
				return PicFragment.instance(picUrls.get(position));
			}
			@Override
			public int getCount() {
				return picUrls.size();
			}
			
		}
		
		public static class PicFragment extends Fragment{
			String picUrl;
			private DisplayImageOptions options;
			
			public static PicFragment instance(String picUrl){
				PicFragment sf = new PicFragment();
				Bundle subargs = new Bundle();
				subargs.putString("picUrl", picUrl);
				sf.setArguments(subargs);
				return sf;
			}
			
			@Override
			public void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);
				Bundle subargs = getArguments();
				if(subargs!=null){
					picUrl = subargs.getString("picUrl");
					options = ActivityMenuUtil.getImageOptions();
				}
			}
			
			@Override
			public View onCreateView(LayoutInflater inflater,
					ViewGroup container, Bundle savedInstanceState) {
				View view = inflater.inflate(R.layout.fragment_pic_show, null);
				ImageView dishesset_test = (ImageView)view.findViewById(R.id.pic_show_img);
				
				ImageLoader imageLoader =ActivityMenuUtil.getImageLoader(getActivity());
//				if(picUrl!=null && !picUrl.equals("")){
				ActivityMenuUtil.loadImage(imageLoader, Constants.BASE_SERVIER_URLS+picUrl, dishesset_test, options,getActivity());
//				}
				return view;
			}
		}
		
}
